import java.util.Arrays;

public class Test1 {

    public static void main1(String[] args) {
        //一般通过new 关键字 实例化对象
        //JAVA当中的数组 就是一个对象

        //静态初始化
        int[] array1 = {1,2,3,4,5,6,7,8,9,10};//这是下面那个的简写
        int[] array2 = new int[]{1,2,3,4,5,6,7,8,9,10};

        //动态初始化
        int[] array3 = new int[10];//没初始化，默认放零
        //int[] array3 = new int[10]{1,2,3,4,5,6,7,8,9,10}; //error

        //先定义再初始化
        int[] array4;
        array4 = new int[]{1,2,3,4,5,6,7,8,9,10};
        //array4 = {1,2,3,4,5,6,7,8,9,10};//error

        String[] array5;
        array5 = new String[10];//默认为null

        char[] array6 = new char[10];//默认为‘\u0000’

        boolean[] array7 = new boolean[10];//默认false
    }

    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9,10};
        System.out.println(array);
        //[I@14ae5a5
        //[表示数组
        //I整型
        //@分割符
        //后面是地址
        //引用变量array是在栈上
        //指向堆上的一块空间

        System.out.println(array[0]);
        System.out.println(array[1]);

        //求数组长度 数组名.length
        System.out.println(array.length);
        System.out.println("====");


        //遍历数组
        //第一种
        //通过下标访问
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }

        System.out.println();
        System.out.println("====");


        //第二种
        //for-each 遍历数组，与下标没有关系
        for (int i : array) {
            System.out.print(i+" ");
        }

        System.out.println();
        System.out.println("====");

        //第三种
        //通过Arrays类里的 toString方法
        //返回一个字符串，只有打印时用
        System.out.println(Arrays.toString(array));


        System.out.println("====");

        //Arrays中的排序方法
        int[] array2 = {5,76,21,3,66};
        Arrays.sort(array2);

        System.out.println(Arrays.toString(array2));
    }

    public static void main3(String[] args) {
        int[] array = null;
        System.out.println(array);//null
        System.out.println(array[0]);//NullPointerException 空指针异常
    }

    public static void main4(String[] args) {
        int[] array1 = {1,2,3,4,5};
        array1[0] = 99;

        int[] array2 = array1;
        array2[0] = 100;

        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }

    public static void main5(String[] args) {
        //二维数组定义与初始化
        int[][] array1 = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};

        int[][] array3 = new int[2][3];

        System.out.println(array1.length);//2

        //三种打印
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("===============");
        for (int[] i : array1) {
            for (int x : i) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
        System.out.println("===============");
        System.out.println(Arrays.deepToString(array1));
        System.out.println("===============");
    }

    public static void main(String[] args) {
        //可以省略列，不能省略行
        int[][] array1 = new int[2][];
        System.out.println(array1);//[[I@14ae5a5
        System.out.println(array1[0]);//null
        System.out.println(array1[1]);//null

        //不规则二维数组
        array1[0] = new int[2];
        array1[1] = new int[4];
        System.out.println(array1[0]);//[I@14ae5a5
        System.out.println(array1[1]);//[I@7f31245a

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        //0 0
        //0 0 0 0

    }
}

